package com.mikhailenko.transportMVC.service.es.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mikhailenko.transportMVC.exeption.es.ESNotFoundException;
import com.mikhailenko.transportMVC.model.es.ReadyES;
import com.mikhailenko.transportMVC.repository.es.ReadyESRepository;
import com.mikhailenko.transportMVC.service.es.ReadyESService;

@Service
public class ReadyESServiceImpl implements ReadyESService {


    @Autowired
    private ReadyESRepository readyESRepository;
    
    @Override
    @Transactional
    public ReadyES create(ReadyES es) {
    	ReadyES createdES = es;
        return readyESRepository.saveAndFlush(createdES);
    }

    @Override
    @Transactional(rollbackFor=ESNotFoundException.class)
    public ReadyES delete(int id) throws ESNotFoundException {
    	ReadyES deletedES = readyESRepository.findOne(id);
        
        if (deletedES == null){
            throw new ESNotFoundException();
        }
        
        readyESRepository.delete(id);
        return deletedES;
    }

    @Override
    @Transactional
    public List<ReadyES> findAll() {
        return readyESRepository.findAll();
    }

    @Override
    public ReadyES update(ReadyES es) throws ESNotFoundException {
    	ReadyES updatedES = readyESRepository.findOne(es.getId());
        
        if (updatedES == null){
            throw new ESNotFoundException();
        }
        
        updatedES.setEsId(es.getEsId());;
        
        return updatedES;
    }

    @Override
    public ReadyES findById(int id) {
        return readyESRepository.findOne(id);
    }

}
